﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class ProductBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public ProductBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string Code)
        {
            if (DAO.T_PRODUCTs.Where(t => t.CODE.ToLower() == Code.ToLower()).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_PRODUCT> GetProductList()
        {
            var list = DAO.T_PRODUCTs.Where(WhereCondition);

            if (OrderCondition != "")
            {
             list=   list.OrderBy(OrderCondition);
            }
            else
            {
                list = list.OrderBy(t=>t.NAME);
            }

            return list;
        }

        public T_PRODUCT GetProductByID(string ID)
        {
            return DAO.T_PRODUCTs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddProduct(T_PRODUCT Product)
        {
            try
            {
                Product.ID = DAO.GetGUID();

                DAO.T_PRODUCTs.InsertOnSubmit(Product);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddProduct(T_PRODUCT Product, out string ProductGuid)
        {
            try
            {
                Product.ID = DAO.GetGUID();
                
                DAO.T_PRODUCTs.InsertOnSubmit(Product);
                DAO.SubmitChanges();

                ProductGuid = Product.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                ProductGuid = "";
                return -1;
            }
        }

        public int UpdateProduct(T_PRODUCT Product)
        {
            try
            {
                T_PRODUCT updateEntity = DAO.T_PRODUCTs.SingleOrDefault(t=>t.ID.ToString()==Product.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                //updateEntity.BRAND = Product.BRAND;
                updateEntity.CODE = Product.CODE;
                updateEntity.MODEL = Product.MODEL;
                updateEntity.NAME = Product.NAME;
               // updateEntity.ORIGIN = Product.ORIGIN;
               // updateEntity.SIZE = Product.SIZE;
               // updateEntity.STANDARD = Product.STANDARD;
                updateEntity.UNIT_ID = Product.UNIT_ID;
                updateEntity.WEIGHT_ID = Product.WEIGHT_ID;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        
        public int DeleteProduct(string ID)
        {
            try
            {
                T_PRODUCT deleteEntity = DAO.T_PRODUCTs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_PRODUCTs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
